package com.xijing.view;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.xijing.model.ProduceInfo;
import com.xijing.model.RoomType;
import com.xijing.service.ProduceService;
import com.xijing.service.RoomTypeService;

public class RoomTypeFrame extends JDialog implements ActionListener  {
	private JTable table;
	private JScrollPane scrollpane;
	private DefaultTableModel tableModel; // 表格模型对象
	private JButton addButton,updateButton,delButton,SelButton;
	private JPanel panel,panel2;
	private JTextField aTextField;
	private RoomTypeService Service =new RoomTypeService();
			
	public RoomTypeFrame(JFrame f,Boolean b)
	{
		super(f,"房间类型管理",b);
		setTitle("房间类型管理");
		this.setBounds(new Rectangle(300, 80, 600, 400));
		tableModel = new DefaultTableModel();
		tableModel.addColumn("房间类型ID");
		tableModel.addColumn("房间类型名称");
		tableModel.addColumn("备注");
		table = new JTable(tableModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 单选
		final JLabel lab=new JLabel();
		table.addMouseListener(new MouseAdapter() { // 鼠标事件
			public void mouseClicked(MouseEvent e) {
				int selectedRow = table.getSelectedRow(); // 获得选中行索引
				if(selectedRow!=-1){lab.setText("当前选中行："+selectedRow);}
				//Object oa = tableModel.getValueAt(selectedRow, 0);
				//Object ob = tableModel.getValueAt(selectedRow, 1);
				
			}
		});
		scrollpane = new JScrollPane(table); // 支持滚动
		getContentPane().add(scrollpane, BorderLayout.CENTER);
		
		
		panel = new JPanel();
		addButton = new JButton("添加"); // 添加按钮
		updateButton = new JButton("修改"); // 修改按钮
		delButton = new JButton("删除");
		addButton.addActionListener(this);
		updateButton.addActionListener(this);
		delButton.addActionListener(this);
		panel.add(lab);
		panel.add(addButton);
		panel.add(updateButton);
		panel.add(delButton);
		getContentPane().add(panel, BorderLayout.SOUTH);
		aTextField= new JTextField(10);
		
		SelButton = new JButton("查询"); // 查询按钮
		SelButton.addActionListener(this);
		panel2 = new JPanel();
		panel2.add(aTextField);
		
		panel2.add(SelButton);
		getContentPane().add(panel2, BorderLayout.NORTH);
		this.setBounds(200, 200, 650, 400);
		BindData("");
		this.setVisible(true);
		
	}
	public void BindData(String KeyStr)
	{
        try 
 	   {    
        	tableModel.setRowCount(0);
           
        	List<RoomType> list =Service.getRoomTypeList(KeyStr);
        	for(int i=0;i<list.size();i++)
        	{
        		RoomType p =list.get(i);
        		tableModel.addRow(new String[]{
        				String.valueOf(p.getSRT_ID()),
        				p.getSRT_Name(),
        				p.getSRT_Remark()
        		});
        	}
 		  
 		 
 	   }
 	   catch (Exception e) 
 	   {
 		   e.printStackTrace();
 	   }
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==SelButton)
		{
			String KeyStr = aTextField.getText();
			BindData(KeyStr);
		}
		if(e.getSource()==addButton)
		{
			new RoomTypeEditFrame(null,true,null);
			BindData("");
		}
		if(e.getSource()==updateButton)
		{
			int selectedRow = table.getSelectedRow(); // 获得选中行索引
			if(selectedRow!=-1)
			{
			   String ID = tableModel.getValueAt(selectedRow, 0).toString();
			   new RoomTypeEditFrame(null,true,ID);
			   BindData("");
			}
		}
		if(e.getSource()==delButton)
		{
		
		}
	}

	
}
